package com.yikaiye.android.yikaiye.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ItemProductLayoutBinding;
import com.yikaiye.android.yikaiye.databinding.ItemProductLayoutLineBinding;
import com.yikaiye.android.yikaiye.http.ApiFactory;
import com.yikaiye.android.yikaiye.model.ProductModel;
import com.yikaiye.android.yikaiye.ui.activity.home.ProductDetailsActivity;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;

public class ProductListAdapter extends BaseQuickAdapter<ProductModel, DataBindingHolder<ItemProductLayoutLineBinding>> {
    private final Context context;

    public ProductListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemProductLayoutLineBinding> bindingDataBindingHolder, int i, @Nullable ProductModel productModel) {
        ItemProductLayoutLineBinding binding = bindingDataBindingHolder.getBinding();
        if (productModel != null) {
            binding.tvTitle.setText(CommonUtil.checkText(productModel.getTitle()));
            binding.tvDes.setText(CommonUtil.checkText(productModel.getSlogan()));
            binding.tvPrice.setText(String.format(context.getString(R.string.price), TextUtils.isEmpty(productModel.getMinPrice()) ? "0.00" : productModel.getMinPrice()));
            binding.tvLinePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            binding.tvLinePrice.setText(String.format(context.getString(R.string.price), TextUtils.isEmpty(productModel.getMaxPrice()) ? "0.00" : productModel.getMaxPrice()));
            Glide.with(context).load(ApiFactory.BASE_IMAGE_URL + productModel.getAppBanner()).placeholder(R.mipmap.default_image).error(R.mipmap.default_image).into(binding.ivProductImg);
            bindingDataBindingHolder.itemView.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.ID_KEY, productModel.getId());
                JumpIntent.jump((Activity) context, ProductDetailsActivity.class, bundle);
            });
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemProductLayoutLineBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_product_layout_line, viewGroup);
    }
}
